<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdapterWebController;
use App\Models\Adv\Adv;
use App\Models\Adv\AdvClassify;
use App\Modules\Auth\Auth;
use App\Modules\Adv\Adv as ApiAdv;
use Illuminate\Http\Request;
//use Illuminate\Pagination\LengthAwarePaginator;
//use Illuminate\Support\Collection;


class AdvController extends AdapterWebController
{

    //广告列表
    public function advList(Request $request){
        if($request->isMethod('get')){
            $adv = $this->appLayerExec(ApiAdv::class, 'advList');
            return view('admin.adv.advertising',['list'=>$adv['res'],'adv'=>1,'advStatus'=>1,'advList'=>1]);
        }
        $postData = array_filter($request->all());
        $adv = $this->appLayerExec(ApiAdv::class, 'advList', $postData);

        return view('admin.adv.advertising',['list'=>$adv['res'],'adv'=>1,'advStatus'=>1,'advList'=>1]);
    }

    //广告启用
    public function enableAdv($id=0){
        if($id == 2) {
            return view('admin.adv.change_adv_status');
        }else{
            \App\Models\Adv\Adv::where('id','=',$id)->update(['status'=>1]);
        }
    }


    //广告禁用
    public function disableAdv($id=0){
        if($id == 3) {
            return view('admin.adv.change_adv');
        }else{
            \App\Models\Adv\Adv::where('id','=',$id)->update(['status'=>0]);
        }
    }

    //广告删除
    public function deleteAdv($id=0){
        if($id == 4) {
            return view('admin.adv.delete_adv');
        }else{
            \App\Models\Adv\Adv::where('id','=',$id)->update(['status'=>-1]);
        }
    }

    // 添加广告
    public function addAdv(Request $request)
    {
        $postData = $request->all();
        //dd($postData);
        if($request->isMethod('get')){            
            return view('admin.adv.add_adv');
        }
        $adv = $this->appLayerExec(ApiAdv::class, 'addAdv', $postData);
        flash('添加成功')->success();
        return view('admin.adv.add_adv');
    }    

    // 编辑广告
    public function editAdv(Request $request ,$id)
    {
        
        if($request->isMethod('get')){            
            $info = \App\Models\Adv\Adv::where('id','=',$id)->first();
            return view('admin.adv.edit_adv',['info'=>$info]);
        }
        $postData = $request->all(); 
        $adv = $this->appLayerExec(ApiAdv::class, 'editAdv', $postData); 
        flash('修改成功')->success();
        $info = \App\Models\Adv\Adv::where('id','=',$postData['id'])->first();
        return view('admin.adv.edit_adv',['info'=>$info]);
    }

}